//
//  OIWorkInfoModel.m
//  EducationOID
//
//  Created by slz on 2017/6/7.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIWorkInfoModel.h"
#import "OIPubListItem.h"

@implementation OIWorkInfoModel
- (Class)classOfItem
{
    return [OIPubListItem class];
}

-(TGMBaseModelActionData *)dataWithActionId:(NSString *)actionId{
    return [TGMBaseModelActionData dataWithApi:kAPI_WORK_SHOW_NEWS_DETAIL httpMethod:kRequestHttpMethodGet params:@{@"id":actionId}];
}

-(BOOL)processServiceSuccRawData:(NSData *)rawData parsedJson:(MFWJson *)jsonObj forTask:(MFWHttpTask *)task{
    NSString *dataId = task.dataIdentifier.dataId;
    [_dataDic setValue:jsonObj.jsonObj forKey:dataId];
    return YES;
}


- (BOOL)processSuccRawData:(NSData *)rawData parsedJson:(MFWJson *)jsonObj forTask:(MFWHttpTask *)task{
    BOOL ret = NO;
    ret = [self processServiceSuccRawData:rawData parsedJson:jsonObj forTask:task];
    if (ret)
    {
        //设置dataStatus
        NSString *dataId = task.dataIdentifier.dataId;
        MFWDataStatus *dataStatus = [self dataStatusForDataId:dataId];
        
        if (task.dataRequestType == kMFWDataRequest_Refresh)
        {// refresh
            dataStatus.refreshDate = [NSDate date];
            dataStatus.statusType = kDataStatus_SuccRefresh;
        }
        else
        {// get more
            dataStatus.statusType = kDataStatus_SuccGetmore;
            
            MFWJson *jsonData = jsonObj;
            NSString *keyOfList = [self keyOfListData];
            if (![MFWStringUtils isEmptyString:keyOfList])
            {
                if ([[jsonData jsonForKey:keyOfList] count] == 0)
                {
                    dataStatus.statusType = kDataStatus_DataEnd;
                }
            }
        }
    }
    return  ret;
}


-(MFWTask *)requestRefresh:(TGMBaseModelActionData *)data
{
    MFWHttpTask *task = [MFWHttpTask task];
    NSString* dataId = data.dataId;
    task.dataIdentifier = [MFWDataIdentifier identifierOfCategory:[self.class categoryName]
                                                           dataId:dataId];
    task.dataRequestType = kMFWDataRequest_Refresh;
    task.block = nil;
    task.userInfo = data;
    task.argsDic = data.params.copy;
    if ([[MFWHttpEngine sharedInstance] addTask:task])
    {
        MFWDataStatus *dataStatus = [self dataStatusForDataId:dataId];
        dataStatus.statusType = kDataStatus_Refreshing;
        
        return task;
    }else{
        return nil;
    }
}

- (MFWHttpRequest*)mfwHttpRequestForTask:(MFWHttpTask*)task;
{
    MFWHttpRequest* request = [MFWHttpRequest dataRequest];
    request.task = task;
    request.httpMethod = kRequestHttpMethodGet;
    TGMBaseModelActionData *actionData = task.userInfo;
    request.requestUrl = actionData.apiUrl;
    request.httpMethod = actionData.httpMethod;
    
    request.requestParams = [NSMutableDictionary dictionaryWithDictionary:task.argsDic];
    
    return request;
}



-(BOOL)bSupportPersistentCache:(NSString *)dataId
{
    return NO;
}
@end
